<template>
  <div class="counter-wrap">
      <div class="counter-btn" @click="minus">-</div>
      <input type="text" v-model="number" @keyup="fixNum">
      <div class="counter-btn" @click="add">+</div>
  </div>
</template>

<script>
export default {
  props:{
      max: {
      type: Number,
      default: 5
    },
    min: {
      type: Number,
      default: 1
    }
  },
  data () {
      return {
          number: this.min
      }
  },
  watch: {
      number () {
          this.$emit('on-change',this.number)
      }
  },
  methods:{
      minus () {
        if (this.number <= this.min) {
            return
        }
        return this.number --
      },
      add () {
        if (this.number >= this.max) {
            return
        }
        return this.number ++
      },
      fixNum() {
        let fix
        if (typeof this.number === 'string') {
            fix = Number(this.number.replace(/\D/g, ''))
        }
        else{
            fix = this.number
        }
        if (fix > this.max || fix < this.min) {
            fix = this.min
        }
        this.number = fix
      }
  }
}
</script>

<style scoped>
.counter-wrap
{
    display: inline-block;
    overflow: hidden;
    vertical-align: middle;
}
.counter-btn{
    float: left;
    /* display: inline-block; */
    border: 1px solid #e3e3e3;
    height: 25px;
    line-height: 25px;
    width: 25px;
    text-align: center;
    cursor: pointer;
}

.counter-wrap input{
    float: left;
    border: none;
    border-top: 1px solid #e3e3e3;
    border-bottom: 1px solid #e3e3e3;
    height: 25px;
    line-height: 25px;
    width: 30px;
    outline: none;
    text-align: center;
}

.counter-btn:hover {
  border-color: #4fc08d;
  background: #4fc08d;
  color: #fff;
}
</style>
